package com.aixingfu.sports.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aixingfu.sports.Adapter.SelectCardTypeAdapter;
import com.aixingfu.sports.MVP.contract.SelectCardTypeContract;
import com.aixingfu.sports.MVP.presenter.SelectCardTypePresenter;
import com.aixingfu.sports.Model.SelectCardTypeModel;
import com.aixingfu.sports.R;
import com.aixingfu.sports.view.SpaceItemDecoration;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orie.librarybase.base.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

//选择卡种
@Route(path = "/main/SelectCardTypeActivity")
public class SelectCardTypeActivity extends BaseActivity implements SelectCardTypeContract.MyView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycle_select_card_type)
    RecyclerView recycleSelectCardType;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_birthday)
    TextView tvBirthday;
    @BindView(R.id.tv_remarks)
    TextView tvRemarks;
    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    private List<String> stringList = new ArrayList<>();
    private SelectCardTypeAdapter selectCardTypeAdapter;
    private SelectCardTypePresenter selectCardTypePresenter;
    private String userid;
    private List<SelectCardTypeModel.DataBean.CardDataBean> card_data = new ArrayList<>();
    private SelectCardTypeModel.DataBean.MemberInfoBean member_info;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_select_card_type;
    }

    public static void startActivityByRoute() {
        ARouter.getInstance().build("/main/SelectCardTypeActivity").navigation();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        tvTitle.setText("选择卡种");

        Bundle bundle = getIntent().getExtras();
        userid = bundle.getString("id", "");
        selectCardTypePresenter = new SelectCardTypePresenter();
        selectCardTypePresenter.attachView(this);
        getSelectCardType();

        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        mSwipeRefreshLayout.setColorSchemeResources(R.color.textColor5, R.color.purple, R.color.yellow1);
        mSwipeRefreshLayout.setRefreshing(true);
        recycleSelectCardType.setLayoutManager(new LinearLayoutManager(SelectCardTypeActivity.this, LinearLayoutManager.VERTICAL, false));
        selectCardTypeAdapter = new SelectCardTypeAdapter(SelectCardTypeActivity.this, R.layout.item_select_card_type, card_data);
        recycleSelectCardType.setAdapter(selectCardTypeAdapter);
        recycleSelectCardType.addItemDecoration(new SpaceItemDecoration(0, 20));
        selectCardTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        selectCardTypeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                int id = view.getId();
                if (id == R.id.tv_renew) {
                    ARouter.getInstance().build("/main/RenewActivity").withString("memberId", card_data.get(position).getMember_id()).withString("memberCardId", card_data.get(position).getMemberCard_id()).withString("card_number", card_data.get(position).getCard_number()).withString("card_category_id", card_data.get(position).getCard_category_id())
                            .withString("invalid_time", card_data.get(position).getInvalid_time())
                            .withString("card_name", card_data.get(position).getCard_name())
                            .withString("username", member_info.getUsername())
                            .withString("mobile", member_info.getMobile()).navigation();
                } else if (id == R.id.tv_upgrade) {
//                    UpgradeActivity.startActivityByRoute();
                    ARouter.getInstance().build("/main/UpgradeActivity")
                            .withString("username", member_info.getUsername())
                            .withString("mobile", member_info.getMobile())
                            .withString("card_name", card_data.get(position).getCard_name())
                            .withString("card_number", card_data.get(position).getCard_number())
                            .withString("create_at", card_data.get(position).getCreate_at())
                            .withString("invalid_time", card_data.get(position).getInvalid_time())
                            .withString("venue_id", card_data.get(position).getVenue_id())
                            .withString("oldCardCategory", card_data.get(position).getOldCardCategory())
                            .withString("memberCard_id", card_data.get(position).getMemberCard_id())
                            .withString("member_id", card_data.get(position).getMember_id())
                            .withString("amount_money", card_data.get(position).getAmount_money()).navigation();
                }
            }
        });

        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (card_data != null) {
                    card_data.clear();
                }
                // 开始刷新，设置当前为刷新状态
                if (mSwipeRefreshLayout != null) {
                    mSwipeRefreshLayout.setRefreshing(false);
                }

                getSelectCardType();
            }
        });
    }

    public void getSelectCardType() {
        if (selectCardTypePresenter.isViewAttached()) {
            HashMap<String, String> map = new HashMap<>();
            map.put("member_id", userid);
            selectCardTypePresenter.getSelectCardType(map);
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void beforeInit() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void showToast(String msg, String state) {
    }

    @Override
    public void setSelectCardType(SelectCardTypeModel data) {
        if (data.getCode() == 1) {
            if (mSwipeRefreshLayout != null) {
                // 加载完数据设置为不刷新状态，将下拉进度收起来
                mSwipeRefreshLayout.setRefreshing(false);
            }
            member_info = data.getData().getMember_info();
            tvName.setText("会员姓名：" + member_info.getUsername());
            tvPhone.setText("手机号：" + member_info.getMobile());
            String birth_date = member_info.getBirth_date();
            if (TextUtils.isEmpty(birth_date)) {
                tvBirthday.setText("生日：");
            } else {
                tvBirthday.setText("生日：" + member_info.getBirth_date());
            }
            String note = member_info.getNote();
            if (TextUtils.isEmpty(note)){
                tvRemarks.setText("备注：");
            }else {
                tvRemarks.setText("备注：" + member_info.getNote());
            }
            card_data = data.getData().getCard_data();
            selectCardTypeAdapter.setNewData(card_data);
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .dontAnimate()
                    .placeholder(R.mipmap.touxiang)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(SelectCardTypeActivity.this)
                    .load(member_info.getAvatar_url())
                    .apply(options)
                    .into(ivHead);
        } else {

        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void hideLoading() {
        dismissLoadingDialog();
    }

    @Override
    public Context getMyContext() {
        return this;
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
